package com.example.teaculture.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.example.teaculture.R;
import com.example.teaculture.activity.FavorDetailActivity;
import com.example.teaculture.adapter.TeaBaseAdapter;
import com.example.teaculture.application.MyApplication;
import com.example.teaculture.model.Favor;
import com.example.teaculture.model.TeaInfo;
import com.example.teaculture.view.RefreshableView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

public class BusinessFragment extends androidx.fragment.app.Fragment {
    RefreshableView refreshableView;

    Handler handler;

    List<TeaInfo> businesses;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.business_fragment, container, false);
        ListView lv_view = view.findViewById(R.id.list_view);


        businesses = MyApplication.getInstance().businesses;
        TeaBaseAdapter teaBaseAdapter = new TeaBaseAdapter(getContext(), businesses);
        lv_view.setAdapter(teaBaseAdapter);

        handler = new Handler(message -> {
            if (message.what == 0) {

                TeaBaseAdapter baseAdapter1 = new TeaBaseAdapter(getContext(), businesses);
                lv_view.setAdapter(baseAdapter1);

            }
            return false;
        });

        lv_view.setOnItemClickListener((adapterView, view1, i, l) -> {
            Favor favor = new Favor();
            favor.setTitle(businesses.get(i).getTitle());
            favor.setUrl(businesses.get(i).getUrl());
            MyApplication.getInstance().favor = favor;
            Intent intent = new Intent(getContext(), FavorDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.putExtra("url", businesses.get(i).getUrl());
            intent.putExtra("title", businesses.get(i).getTitle());
            startActivity(intent);
        });

        refreshableView = (RefreshableView) view.findViewById(R.id.refreshable_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    businesses.clear();
                    Document doc = Jsoup.connect("http://zhannei.baidu.com/cse/search?q=%E8%8C%B6%E7%BB%8F%E8%90%A5&p=1&s=2942127813356630428&nsid=&entry=1").get();
                    Elements elements = doc.select("div.result");
                    for (Element element : elements) {
                        String title = element.select("h3.c-title").select("a").text();
                        String url = element.select("h3.c-title").select("a").attr("href");
                        String image = element.select("td").select("img").attr("src");
                        String info = element.select("div.c-abstract").text();
                        String date = element.select("div").select("span.c-showurl").text().split(" ")[1];
                        TeaInfo teaBusiness = new TeaInfo(title, info, url, date, image);
                        businesses.add(teaBusiness);
                    }
                    Message message = Message.obtain();
                    message.what = 0;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshableView.finishRefreshing();
            }
        }, 3);

        return view;
    }
}
